package com.googlecode.perfectable.visuable.configuration.guice;

import com.google.inject.Injector;
import com.googlecode.perfectable.visuable.configuration.StandardConfigurator;
import com.googlecode.perfectable.visuable.event.Event;
import com.googlecode.perfectable.visuable.event.EventListener;
import com.googlecode.perfectable.visuable.visual.Visual;
import com.googlecode.perfectable.visuable.visual.controller.Controller;

public class GuiceConfigurator extends StandardConfigurator {

	@Override
	public <C extends Visual, T extends Controller<? super C>> T createController(Class<T> controllerClass,
			Class<C> visualClass) {
		final T controller = super.createController(controllerClass, visualClass);
		this.injectMembers(controller);
		return controller;
	}

	@Override
	public <E extends Event, T extends EventListener<? super E>> T createListener(Class<T> listenerClass,
			Class<E> eventClass) {
		final T listener = super.createListener(listenerClass, eventClass);
		this.injectMembers(listener);
		return listener;
	}

	@SuppressWarnings("static-method")
	protected void injectMembers(Object target) {
		Injector injector = GuiceInjectorListener.injector();
		injector.injectMembers(target);
	}
}
